package cn.shengongying.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * author: RXK
 * date: 2021/12/29 星期三
 * desc:
 */
public class DateUtils {

    public static final String date = "yyyyMMdd";

    public static final String date_time_ss = "yyyyMMddHHmmss";

    public static final String date_time_minus = "yyyy-MM-dd HH:mm";

    public static final String date_time_minus_sec = "yyyy-MM-dd HH:mm:ss";


    private DateUtils() {
    }

    public static Date string2Date(String dateStr, String pattern) {
        return Date.from(LocalDateTime.from(DateTimeFormatter.ofPattern(pattern)
                        .parse(dateStr))
                .atZone(ZoneId.systemDefault())
                .toInstant());
    }

    public static String date2Str(Date date, String pattern) {
        return DateTimeFormatter.ofPattern(pattern)
                .format(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()));
    }

    /**
     * 自当前日期起 增加/减少置顶的单位或者天数
     *
     * @param count ：数量
     * @param chronoUnit  ：单位
     * @return
     */
    public static Date add(int count, ChronoUnit chronoUnit) {
        return Date.from(LocalDateTime.now(ZoneId.systemDefault())
                .plus(count, chronoUnit)
                .toInstant(ZoneOffset.UTC));
    }

    public static Duration endDateDuration() {
       return Duration.between(LocalTime.now(), LocalTime.MAX);
    }

    public static Date addDate(Integer day) {
        return Date.from(LocalDateTime.now(ZoneId.systemDefault())
                .plusDays(day)
                .toInstant(ZoneOffset.UTC));
    }
}
